

package com.maoshi.shop.coupon.api.listener;

import com.maoshi.shop.bean.app.dto.CouponOrderDto;
import com.maoshi.shop.bean.app.dto.ShopCartItemDto;
import com.maoshi.shop.bean.app.dto.ShopCartOrderDto;
import com.maoshi.shop.bean.app.dto.ShopCartOrderMergerDto;
import com.maoshi.shop.bean.event.ConfirmOrderEvent;
import com.maoshi.shop.bean.event.PlatformConfirmOrderEvent;
import com.maoshi.shop.bean.order.ConfirmOrderOrder;
import com.maoshi.shop.common.util.Arith;
import com.maoshi.shop.coupon.api.util.ChooseCouponHelper;
import com.maoshi.shop.coupon.comment.service.CouponService;
import com.maoshi.shop.security.api.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 确认订单信息时的优惠券的相关操作，计算优惠券 优惠金额
 *
 * @author LGH
 */
@Component("couponConfirmOrderListener")
@AllArgsConstructor
public class ConfirmOrderListener {

    private final CouponService couponService;

    /**
     * 将店铺下的所有商品归属到该店铺的购物车当中
     *
     * @param event#getShopCart()       购物车
     * @param event#shopCartItemDtoList 该购物车的商品
     * @return 是否继续组装
     */
    @EventListener(ConfirmOrderEvent.class)
    @Order(ConfirmOrderOrder.COUPON)
    public void couponConfirmOrderEvent(ConfirmOrderEvent event) {
        String userId = SecurityUtils.getUser().getUserId();

        ShopCartOrderDto shopCartOrderDto = event.getShopCartOrderDto();

        // 该店铺中的购物车商品列表
        List<ShopCartItemDto> shopCartItems = event.getShopCartItems();

        // 获取用户有效的优惠券
        List<CouponOrderDto> shopCoupons = couponService.getCouponListByShopIds(userId, shopCartOrderDto.getShopId());

        // 用户选中的所有优惠券
        List<Long> couponIds = event.getOrderParam().getCouponIds();

        Integer userChangeCoupon = event.getOrderParam().getUserChangeCoupon();

        ChooseCouponHelper chooseCouponHelper = new ChooseCouponHelper(shopCartItems, shopCoupons, couponIds, userChangeCoupon).invoke();

        if (chooseCouponHelper.getChooseCoupon() != null) {
            // 最后组装订单信息
            assembleOrder(shopCartOrderDto, chooseCouponHelper.getCouponReduce());
        }

        shopCartOrderDto.setCoupons(shopCoupons);
    }


    /**
     * 进行最后订单的组装
     *
     * @param shopCartOrderDto 该店铺的购物车订单
     */
    private void assembleOrder(ShopCartOrderDto shopCartOrderDto,double couponReduce) {
        // 订单优惠券优惠金额要小于订单实际金额
        shopCartOrderDto.setCouponReduce(Math.min(couponReduce, shopCartOrderDto.getActualTotal()));
        shopCartOrderDto.setActualTotal(Arith.sub(shopCartOrderDto.getActualTotal(), shopCartOrderDto.getCouponReduce()));
        shopCartOrderDto.setShopReduce(Arith.add(shopCartOrderDto.getShopReduce(), shopCartOrderDto.getCouponReduce()));
    }



    /**
     * 计算平台优惠券
     */
    @EventListener(PlatformConfirmOrderEvent.class)
    @Order(ConfirmOrderOrder.PLATFORM_COUPON)
    public void calculatePlatformCoupon(PlatformConfirmOrderEvent event){
        String userId = SecurityUtils.getUser().getUserId();

        // 完整的订单信息
        ShopCartOrderMergerDto shopCartOrderMergerDto = event.getShopCartOrderMergerDto();
        // 所有商品 item
        List<ShopCartItemDto> shopAllShopCartItems = event.getAllCartItem();

        // 获取用户可用平台优惠券
        List<CouponOrderDto> couponList = couponService.getCouponListByShopIds(userId, 0L);

        List<ShopCartOrderDto> shopCartOrders = shopCartOrderMergerDto.getShopCartOrders();
        ChooseCouponHelper chooseCouponHelper = new ChooseCouponHelper(shopAllShopCartItems, couponList, event.getOrderParam().getCouponIds(), event.getOrderParam().getUserChangeCoupon()).invoke();


        if (chooseCouponHelper.getChooseCoupon() != null) {
            for (ShopCartOrderDto shopCartOrder : shopCartOrders) {
                // 组装店铺订单信息
                assembleOrder(shopCartOrder, chooseCouponHelper.getCouponReduce());
            }
            // 最后组装订单信息
            assembleOrder(shopCartOrderMergerDto,chooseCouponHelper.getCouponReduce());
        }
        shopCartOrderMergerDto.setCoupons(couponList);
    }


    /**
     * 进行最后订单的组装
     *
     * @param shopCartOrderMergerDto 整个平台的购物车订单
     */
    private void assembleOrder(ShopCartOrderMergerDto shopCartOrderMergerDto,double couponReduce) {
        // 订单优惠券优惠金额要小于订单实际金额
        double minCouponReduce = Math.min(couponReduce, shopCartOrderMergerDto.getActualTotal());
        shopCartOrderMergerDto.setOrderReduce(Arith.add(shopCartOrderMergerDto.getOrderReduce(), minCouponReduce));
        shopCartOrderMergerDto.setActualTotal(Arith.sub(shopCartOrderMergerDto.getActualTotal(), minCouponReduce));
    }
}
